﻿using NxStoreManage;

using System;
using System.Collections.Generic;
using System.Linq;

namespace LengthAiCalculate
{
    public partial class AddCutGroup : BaseForm
    {
        public AddCutGroup()
        {
            InitializeComponent();
        }

        public string mid = "";

        public Action<bool> SuccessAction;

        private void button16_Click(object sender, EventArgs e)
        {
            List<string> sql = new List<string>();
            decimal.TryParse(textBox17.Text, out var l1);
            decimal.TryParse(textBox16.Text, out var l2);
            decimal.TryParse(textBox9.Text, out var l3);
            decimal.TryParse(textBox1.Text, out var l4);
            int.TryParse(textBox10.Text, out var num1);
            int.TryParse(textBox4.Text, out var num2);
            int.TryParse(textBox3.Text, out var num3);
            int.TryParse(textBox2.Text, out var num4);
            decimal.TryParse(textBox7.Text, out var _lg);
            int.TryParse(textBox8.Text, out var _num);
            if (l1 > 0 && num1 > 0)
            {
                sql.Add($@"IF EXISTS (SELECT * FROM [PM.AiJh] WHERE mId='{mid}' AND lg={l1})UPDATE [PM.AiJh] SET num=num-{num1 * _num} WHERE mId='{mid}' AND lg={l1} ELSE INSERT INTO [PM.AiJh]
                (
                    lg,
                    num,
                    isdel,
                    mId,
                    numBack
                )
                VALUES
                (   {l1},
                    -{num1 * _num},
                    0,
                    '{mid}',
                    0
                    )");
            }
            if (l2 > 0 && num2 > 0)
            {
                sql.Add($@"IF EXISTS (SELECT * FROM [PM.AiJh] WHERE mId='{mid}' AND lg={l2})UPDATE [PM.AiJh] SET num=num-{num2 * _num} WHERE mId='{mid}' AND lg={l2} ELSE INSERT INTO [PM.AiJh]
                (
                    lg,
                    num,
                    isdel,
                    mId,
                    numBack
                )
                VALUES
                (   {l2},
                    -{num2 * _num},
                    0,
                    '{mid}',
                    0
                    )");
            }
            if (l3 > 0 && num3 > 0)
            {
                sql.Add($@"IF EXISTS (SELECT * FROM [PM.AiJh] WHERE mId='{mid}' AND lg={l3})UPDATE [PM.AiJh] SET num=num-{num3 * _num} WHERE mId='{mid}' AND lg={l3} ELSE INSERT INTO [PM.AiJh]
                (
                    lg,
                    num,
                    isdel,
                    mId,
                    numBack
                )
                VALUES
                (   {l3},
                    -{num3 * _num},
                    0,
                    '{mid}',
                    0
                    )");
            }
            if (l4 > 0 && num4 > 0)
            {
                sql.Add($@"IF EXISTS (SELECT * FROM [PM.AiJh] WHERE mId='{mid}' AND lg={l4})UPDATE [PM.AiJh] SET num=num-{num4 * _num} WHERE mId='{mid}' AND lg={l4} ELSE INSERT INTO [PM.AiJh]
                (
                    lg,
                    num,
                    isdel,
                    mId,
                    numBack
                )
                VALUES
                (   {l4},
                    -{num4 * _num},
                    0,
                    '{mid}',
                    0
                    )");
            }
            if (sql.Any())
            {
                List<string> c = new List<string>();
                if (l1 > 0 && num1 > 0)
                {
                    c.Add($"{l1}*{num1}");
                }
                if (l2 > 0 && num2 > 0)
                {
                    c.Add($"{l2}*{num2}");
                }
                if (l3 > 0 && num3 > 0)
                {
                    c.Add($"{l3}*{num3}");
                }
                if (l4 > 0 && num4 > 0)
                {
                    c.Add($"{l4}*{num4}");
                }
                string cut = $"{_lg}={string.Join("+", c)}";
                sql.Add($"UPDATE [PM.AiSc] SET num=num-{_num} WHERE mID='{mid}' AND lg={_lg}");
                sql.Add($"IF EXISTS (SELECT * FROM [PM.AiResult] WHERE mID = '{mid}' AND cutGroup = '{cut}') UPDATE [PM.AiResult] SET num = num + {_num},numBack=numBack+{_num},isUserAdd=1 WHERE mID = '{mid}' AND cutGroup = '{cut}' ELSE INSERT INTO [PM.AiResult] (cutGroup,num,mLength,twoCutNum,opType,mID,isAbandon,isUserAdd,numBack) VALUES ('{cut}',{_num},{_lg},0,'ad','{mid}',0,1,{_num})");
                if (nxsqlHelper.ExcuteSql(sql) > 0)
                {
                    SuccessAction?.Invoke(true);
                    ShowMsg("添加成功！");
                }
            }
        }

        private void SumLength()
        {
            decimal.TryParse(textBox17.Text, out var l1);
            decimal.TryParse(textBox16.Text, out var l2);
            decimal.TryParse(textBox9.Text, out var l3);
            decimal.TryParse(textBox1.Text, out var l4);
            int.TryParse(textBox10.Text, out var num1);
            int.TryParse(textBox4.Text, out var num2);
            int.TryParse(textBox3.Text, out var num3);
            int.TryParse(textBox2.Text, out var num4);
            l1 *= 1.0m;
            l2 *= 1.0m;
            l3 *= 1.0m;
            decimal l = l1 * num1 + l2 * num2 + l3 * num3 + l4 * num4;
            textBox7.Text = l.ToString();
            button16.Enabled = true;
            decimal.TryParse(textBox7.Text, out var _lg);
            int.TryParse(textBox8.Text, out var _num);
            if (_num <= 0)
            {
                button16.Enabled = false;
            }
        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {
            SumLength();
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            SumLength();
        }
    }
}